package com.shipwe.loans.device.util

import android.annotation.SuppressLint
import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.net.ConnectivityManager
import android.os.Build
import android.os.SystemClock
import android.provider.Settings
import android.telephony.TelephonyManager
import java.util.Locale
import java.util.TimeZone

object DeviceShipweGeneralUtil {
    @SuppressLint("HardwareIds")
    fun getAndroidID(context: Context): String {
        return Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID) ?: ""
    }

    fun getPhoneType(context: Context): String {
        return try {
            val telephonyManager =
                context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
            when (telephonyManager.phoneType) {
                TelephonyManager.PHONE_TYPE_GSM -> "GSM"
                TelephonyManager.PHONE_TYPE_CDMA -> "CDMA"
                TelephonyManager.PHONE_TYPE_SIP -> "SIP"
                else -> ""
            }
        } catch (e: Exception) {
            e.printStackTrace()
            ""
        }
    }

    fun getLanguage(): String {
        return try {
            Locale.getDefault().language
        } catch (e: Exception) {
            e.printStackTrace()
            ""
        }
    }

    fun getDisplayLanguage(): String {
        return try {
            Locale.getDefault().displayLanguage
        } catch (e: Exception) {
            e.printStackTrace()
            ""
        }
    }

    fun getIso3Language(): String {
        return try {
            Locale.getDefault().isO3Language
        } catch (e: Exception) {
            e.printStackTrace()
            ""
        }
    }

    fun getIso3Country(): String {
        return try {
            Locale.getDefault().isO3Country
        } catch (e: Exception) {
            e.printStackTrace()
            ""
        }
    }

    fun getTimeZoneId(): String {
        return try {
            TimeZone.getDefault().id
        } catch (e: Exception) {
            e.printStackTrace()
            ""
        }
    }

    fun getNetworkType(context: Context): String {
        return try {
            val connectivityManager =
                context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val network = connectivityManager.activeNetworkInfo
            if (network != null && network.isConnected) {
                when (network.type) {
                    ConnectivityManager.TYPE_WIFI -> "WIFI"
                    ConnectivityManager.TYPE_MOBILE -> "MOBILE"
                    else -> ""
                }
            } else {
                ""
            }
        } catch (e: Exception) {
            e.printStackTrace()
            ""
        }
    }

    fun getSimOperatorName(context: Context): String {
        return try {
            val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
            telephonyManager.networkOperatorName.safe()
        } catch (e: Exception) {
            e.printStackTrace()
            ""
        }
    }

    fun getIsDebug(context: Context): String {
        return try {
            val applicationInfo = context.applicationInfo
            if (applicationInfo != null && applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE != 0) {
                "YES"
            } else {
                "NO"
            }
        } catch (e: Exception) {
            e.printStackTrace()
            "NO"
        }
    }

    fun getElapsedRealtime(): String {
        return try {
            SystemClock.elapsedRealtime().toString()
        } catch (e: Exception) {
            e.printStackTrace()
            ""
        }
    }

    fun getLastBootTime(): String {
        return try {
            (System.currentTimeMillis() - SystemClock.elapsedRealtime()).toString()
        } catch (e: Exception) {
            e.printStackTrace()
            ""
        }
    }

    fun getVersionName(context: Context): String {
        return try {
            val packageInfo = context.packageManager.getPackageInfo(
                context.packageName,
                PackageManager.GET_ACTIVITIES
            )
            packageInfo.versionName ?: "1.0"
        } catch (e: Exception) {
            e.printStackTrace()
            "1.0"
        }
    }

    fun getVersionCode(context: Context): String {
        return try {
            val packageInfo = context.packageManager.getPackageInfo(
                context.packageName,
                PackageManager.GET_ACTIVITIES
            )
            packageInfo.versionCode.toString()
        } catch (e: Exception) {
            e.printStackTrace()
            "1"
        }
    }

    fun getSimCardState(context: Context): String {
        return try {
            val telephonyManager =
                context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
            when (telephonyManager.simState) {
                TelephonyManager.SIM_STATE_ABSENT -> "NO_SIM"
                TelephonyManager.SIM_STATE_UNKNOWN -> "UNKNOWN"
                TelephonyManager.SIM_STATE_NETWORK_LOCKED -> "NETWORK_LOCKED"
                TelephonyManager.SIM_STATE_PIN_REQUIRED -> "PIN_LOCKED"
                TelephonyManager.SIM_STATE_PUK_REQUIRED -> "PUK_LOCKED"
                TelephonyManager.SIM_STATE_READY -> "YES"
                TelephonyManager.SIM_STATE_NOT_READY -> "NOT_READY"
                TelephonyManager.SIM_STATE_PERM_DISABLED -> "DISABLED"
                TelephonyManager.SIM_STATE_CARD_IO_ERROR -> "PRESENT_BUT_FAULT"
                TelephonyManager.SIM_STATE_CARD_RESTRICTED -> "CARRIER_RESTRICTIONS"
                else -> "NO_SIM"
            }
        } catch (e: Exception) {
            e.printStackTrace()
            "NO_SIM"
        }
    }

    fun getCpuModel(): String {
        return try {
            Build.SUPPORTED_ABIS[0]
        } catch (e: Exception) {
            e.printStackTrace()
            ""
        }
    }

    fun getDeviceHeight(context: Context): String {
        return try {
            context.resources.displayMetrics.heightPixels.toString()
        } catch (e: Exception) {
            e.printStackTrace()
            ""
        }
    }

    fun getDeviceWidth(context: Context): String {
        return try {
            context.resources.displayMetrics.widthPixels.toString()
        } catch (e: Exception) {
            e.printStackTrace()
            ""
        }
    }

    fun getArea(): String {
        return Locale.getDefault().getISO3Country()
    }
}
